package com.boxin.ims.modules.momarketing.web.front;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boxin.framework.base.dao.DaoHelper;
import com.boxin.ims.modules.momarketing.entity.CompanyNews;
import com.boxin.ims.modules.momarketing.entity.MicroPage;
import com.boxin.ims.modules.momarketing.entity.MicroProduct;
import com.boxin.ims.modules.momarketing.entity.MicroSite;
import com.boxin.ims.modules.momarketing.entity.Project;
import com.boxin.ims.modules.momarketing.entity.ProjectMenu;
import com.boxin.ims.modules.momarketing.entity.ProjectVisit;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.CompanyNewsService;
import com.boxin.ims.modules.momarketing.service.MicroPageService;
import com.boxin.ims.modules.momarketing.service.MicroProductCategoryService;
import com.boxin.ims.modules.momarketing.service.MicroProductService;
import com.boxin.ims.modules.momarketing.service.ProjectMenuService;
import com.boxin.ims.modules.momarketing.service.ProjectService;
import com.boxin.ims.modules.momarketing.service.ProjectVisitService;
import com.boxin.ims.modules.wechat.entity.Product;
import com.boxin.ims.modules.wechat.service.ProductService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;

@Controller
@RequestMapping(value= Global.MOBILE_SITE_PATH + "/mom")
public class MobileSiteController extends BaseController {
	
	private Logger logger = Logger.getLogger(MobileSiteController.class);
	
	@Autowired
	ProjectService projectService;
	
	@Autowired
	private DaoHelper daoHelper;
	
	@Autowired
	ProductService productService;
	
	@Autowired
	SystemService systemService;
	
	@Autowired
	MicroPageService microPageService;
	
	@Autowired
	private ProjectVisitService projectVisitService;
	
	@Autowired
	private MicroProductCategoryService microProductCategoryService;
	
	@Autowired
	private MicroProductService microProductService;
	
	@Autowired
	private CompanyNewsService companyNewsService;
	
	@Autowired
	private ProjectMenuService projectMenuService;
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-28  上午11:09:42
	 * @function <p> 微网站首页入口 </p>
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "pro/{id}" + Global.URL_SUFFIX)
	public String viewSite(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		
		String menuId = request.getParameter("menuId");
		if(StringUtils.isEmpty(menuId)){
			menuId = "1";
		}
		
		//保存访问 信息
		logger.debug("ok........");
		Project project = projectService.get(id);
		
		QRCode qrCode = project.getQrCode();
		
		ProjectVisit pv = new ProjectVisit();
		pv.setProject(project);
		pv.setBrowser(request.getHeader("user-agent"));
		pv.setIp(request.getRemoteHost());
		pv.setMobile(request.getParameter("mobile"));
		pv.setQq(request.getParameter("qq"));
		pv.setMenuId(Long.parseLong(menuId));
		
		projectVisitService.save(pv);
		
		//行业模板
		String theme =  getProjectTheme(project) ;
				
				
		//判断是否失效
		if(BaseEntity.DEL_FLAG_DELETE.equals(project.getDelFlag())){
			request.setAttribute("errorInfo", "您访问的页面已过期!");
			return "modules/ims/front/mobile/themes/"+theme+"/index";
		}
		
		
		request.setAttribute("project", project);
		request.setAttribute("qrCode", qrCode);
		
		////
		String toPage = "index";
		ProjectMenu projectMenu =	projectMenuService.get(Long.parseLong(menuId));
		if(projectMenu!=null){
			//首页
			if(MicroSite.INDEX.equals(projectMenu.getType())){			//首页
				
			}else if(MicroSite.ABOUT.equals(projectMenu.getType())){	//关于我们
				
			}else if(MicroSite.CONTACT.equals(projectMenu.getType())){	//联系我们
				
			}else if(MicroSite.LOTTERY.equals(projectMenu.getType())){	//投资
				
			}else if(MicroSite.NEWS.equals(projectMenu.getType())){		//新闻动态
				
			}else if(MicroSite.SURVEY.equals(projectMenu.getType())){	//调查
				
			}else if(MicroSite.PRODUCT.equals(projectMenu.getType())){	//产品列表
				List<MicroProduct> allMicroProductList = 	microProductService.findMicroProductsByProjectId(project.getId());
				request.setAttribute("allMicroProductList", allMicroProductList);
			}
			
			toPage = projectMenu.getType();
		}
		
		
		//初始化菜单
		initNavigations(id,request);
		
		//推荐产品
		List<MicroProduct> microProductList = 	microProductService.findIndexProductsByProjectId(project.getId());
		request.setAttribute("productList", microProductList);
		
		
		//公司动态
		List<CompanyNews> newsList = companyNewsService.findByProjectId(project.getId());
		request.setAttribute("newsList", newsList);
		
		
		return "modules/ims/front/mobile/themes/"+theme+"/"+toPage;
	}
	
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-25  上午10:05:00
	 * @function <p> 获取微网站的  模板 </p>
	 * @param project
	 * @return
	 */
	private String getProjectTheme(Project project){
		//行业模板
		String theme =  "default" ;
		if(project.getPageTemplate() != null  && StringUtils.isNotEmpty(project.getPageTemplate().getTheme())){
			theme = project.getPageTemplate().getTheme();
		}
		return theme;
	}

	
	/**
	 * @author Jakemanse
	 * @time 2013-6-28  下午11:49:10
	 * @function <p> 查看产品 页 </p>
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "prod/{id}" + Global.URL_SUFFIX)
	public String viewProduct(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		Product product = new Product();
		User user = systemService.getUser(id);
		product.setUser(user);
		List<Product> productList =  productService.find( product);
		
		request.setAttribute("productList", productList);
		
		return  "modules/ims/front/mobile/themes/default/product";
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-28  上午11:08:31
	 * @function <p> 查看微网站详细 </p>
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "view/{id}" + Global.URL_SUFFIX)
	public String viewMricoPage(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		MicroPage microPage =	microPageService.get(id);
		request.setAttribute("microPage", microPage);
		String theme = "default";
		//初始化菜单
		try {
			request.setAttribute("project", microPage.getProject());
			initNavigations(microPage.getProject().getId(),request);
			//Theme
			theme = getProjectTheme(microPage.getProject());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "modules/ims/front/mobile/themes/"+theme+"/viewMicroPage";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-27  上午12:37:33
	 * @function <p> 查看活动，新闻明细  </p>
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "viewNews/{id}" + Global.URL_SUFFIX)
	public String viewNews(@PathVariable Long id,HttpServletRequest request,HttpServletResponse response){
		
		String theme = "default";
		try {
			CompanyNews news =	companyNewsService.get(id);
			request.setAttribute("news", news);
			request.setAttribute("project", news.getProject());
			
			//初始化菜单
			initNavigations(news.getProject().getId(),request);
			
			//Theme
			theme = getProjectTheme(news.getProject());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "modules/ims/front/mobile/themes/"+theme+"/viewNews";
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-11-24  上午12:28:36
	 * @function <p>初始化导航  </p>
	 * @param projectId
	 * @param request
	 */
	private void initNavigations(Long projectId,HttpServletRequest request){
		String menuId = request.getParameter("menuId");
		
		//处理导航
		List<Map<String,Object>> projectMenus = daoHelper.queryForList("com.boxin.ims.mom.selectProjectMenus", projectId);
		
		//设置显示标题
		for(Map map:projectMenus){
			if( map.get("id").toString().equals(menuId)){
				request.setAttribute("menuName", map.get("name"));
			}
		}
		
		
		//自定义航页
		List<MicroPage> microPageList = 	microPageService.findMicropagesByProjectId(projectId);
		
		request.setAttribute("menus", projectMenus);
		request.setAttribute("microPageList", microPageList);		
	}
	
	
	
	
	

}
